package hibernatemock.persistence;

import hibernatemock.ReflectionUtil;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import javax.persistence.Entity;

import lombok.Getter;
import lombok.Setter;

public class PropertyDescription {

	private ReflectionUtil reflectionUtil = new ReflectionUtil();
	@Getter @Setter private Object rootObject;
	@Getter @Setter private Boolean isRootObjectEntity = false;

	@Getter @Setter private Field field;
	@Getter @Setter private Object fieldObject;
	@Getter @Setter private Object id;
	@Getter @Setter private Boolean isId = false;
	@Getter @Setter private Boolean isAutoIncrementId = false;
	@Getter @Setter private Boolean isCollection = false;

	public void setValue(Number number) {
		reflectionUtil.setProperty(rootObject, field, number);
	}
	
	public void setAgain() {
		reflectionUtil.setProperty(rootObject, field, fieldObject);
	}

	
	public Object getRealFieldObject(){
		if (isCollection){
			return ((Collection<?>)fieldObject).iterator().next();
		}else{
			return fieldObject;
		}
	}
	
	public boolean isFieldEntity() {
		if (isCollection == true) {
			return getCollectionGenericType().isAnnotationPresent(Entity.class);
		} else {
			return field.getType().isAnnotationPresent(Entity.class);
		}
	}

	public Class<?> getCollectionGenericType() {
		return (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
	}
}
